Ut.urlUtils = {
    getRootPath: function () {
        var origin = location.protocol + '//' + this.getHostAndPort() + '/service-center/';
        return origin;

        // TODO
        /*var origin = location.protocol + '//' + location.hostname + ':' + location.port;
         var pathName = location.pathname;
         var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
         var rootPath = origin + projectName + '/';
         return rootPath;*/
    },

    getHostAndPort: function () {
        var port = '';
        if (location.port) {
            port = ':' + location.port;
        }
        return window.location.hostname + port;
    },

    openUrl: function (url) {
        // case 1081580: 在FF中\不会被当成/，而会转成‘_’ --ldzhu
        url = url.replace(/\\/g, '/');
        // TODO dev
        url = this.getRootPath() + url;
        // url = 'http://10.168.17.68:8080/service-center/' + url;

        //截取文件名
        var strArr = url.split(/\//g);
        var fileName = strArr[strArr.length - 1];

        $.ajax({
            url: url,
            type: 'GET',
            xhrFields: {
                responseType: 'blob'
            },
            dataType: 'binary',
            success: function (data) {
                var blob = new Blob([data], {type: "application/octet-stream"});
                //兼容IE浏览器
                if (window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveOrOpenBlob(blob, fileName);
                } else {
                    //火狐、chrome浏览器通过a标签进行下载
                    var a = document.createElement("a");
                    a.id = 'exppub';
                    //保证pdf和html文件不在浏览器中打开，而是下载
                    a.download = fileName;
                    a.href = URL.createObjectURL(blob);
                    document.body.appendChild(a);
                    var alink = document.getElementById('exppub');
                    alink.click();
                    alink.parentNode.removeChild(a);
                }
            },
            error: function () {
                Ut.dialog.tip(I18N.TIP.t29);
            }
        });
    },

    // 将dataURL转化为Blob类型，常用于canvas绘图相关
    dataURLtoBlob: function (dataurl) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], {type: mime});
    }
};